var routers = [
    { path: '/login', component: 'components', name:'login' },
    { path: '/404', component: 'components', name:'404' },
    {
      path: '/',
      component: 'Layout',
      redirect: '/dashboard',
      name: 'home',
      children: [
        {
        path: 'dashboard',
        name: 'dashboard'
        }
        ]
    },
    {
      path: '/demoadmin',
      component: 'Layout',
      name: 'demoadmin',
      children: [
        {
          path: 'demo1',
          name: 'demo1',
          children: [
            {
              path: 'demo3',
              name: 'demo3'
            },
            {
                path: 'demo4',
                name: 'demo4'
              }
          ]
        },
        {
            path: 'demo2',
            name: 'demo2'
        }
      ]
    },
    {
      path: '/print',
      component: 'Layout',
      name:'print',
      children: [
        {
          path: 'pdemo',
          name: 'pdemo'
        }
      ]
    },
    {
      path: '/tinymceDemo',
      component: 'Layout',
      name:'tinymce'
    },
    { path: '*', redirect: '/404', hidden: true, name:'all' }
  ]

// console.log('处理之前的routers：')
// console.log(JSON.stringify(routers))

const auth = {
    demo1:{
        auth_list: true,
        menu_update:false
    },
    demo2:{
        auth_list: false,
        menu_update:false
    },
    demo4:{
        auth_list: true,
        menu_update:false
    }
}
const whiteList = ['/login','/404','/','*']
function handleRouter(routers) {
    for (var i in routers) {
        if (routers[i].children && routers[i].children.length > 0) {
            handleRouter(routers[i].children)
            var len = routers[i].children.length
            var false_len = 0
            for (let j in routers[i].children) {
                if (routers[i].children[j].hidden) {
                    false_len ++
                }
            }
            if (len == false_len) {
                routers[i].hidden = true
            } else {
                routers[i].hidden = false
            }
        } else {
            if (whiteList.indexOf(routers[i].path) == -1) {
                if(auth[routers[i].name]) {
                    if (auth[routers[i].name].auth_list) {
                        routers[i].hidden = false
                    } else {
                        routers[i].hidden = true
                    }
                } else {
                    routers[i].hidden = true
                }
            }
        }
    }
}


// handleRouter(routers)

// console.log('递归处理过后的routers：')
// console.log(JSON.stringify(routers))

function handleOneRouter(router) {
  if (!router.children) {
    if (whiteList.indexOf(router.path) == -1) {
      if(auth[router.name]) {
        if (auth[router.name].auth_list) {
          router.hidden = false
        } else {
          router.hidden = true
        }
      } else {
        router.hidden = true
      }
    }
    return 1
  } else {
    var count = 1
    for(var i in router.children) {
      count += handleOneRouter(router.children[i])
      if (router.children[i].children){
        var len = router.children[i].children.length
        var false_len = 0
        for (let j in router.children[i].children) {
          if (router.children[i].children[j].hidden) {
            false_len ++
          }
        }
        if (len == false_len) {
          router.children[i].hidden = true
        } else {
          router.children[i].hidden = false
        }
      }
    }
    return count
  }
}
var need_handle_routers = {children: routers}
console.log(JSON.stringify(need_handle_routers))
var all_count = handleOneRouter(need_handle_routers)
console.log(all_count)
console.log(JSON.stringify(need_handle_routers))
// console.log(routers[2].children.length)
// console.log(JSON.stringify(routers[2]))